package com.csii.mobilebanking;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Parcelable;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.Toast;

import com.csii.mobilebanking.Strings.Extra;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.assist.FailReason;
import com.nostra13.universalimageloader.core.assist.SimpleImageLoadingListener;

public class ImagePagerAdapter extends PagerAdapter implements OnClickListener{

	private String[] images;
	private LayoutInflater inflater;
	DisplayImageOptions options;
	private Context context;
	private int posittion;
	private boolean imageShow=false;
	
	protected ImageLoader imageLoader = ImageLoader.getInstance();
	ImagePagerAdapter(Context context,DisplayImageOptions option,String[] images) {
		inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		this.options=option;
		this.context=context;
		this.images = images;
	}
	@Override
	public void destroyItem(ViewGroup container, int position, Object object) {
		((ViewPager) container).removeView((View) object);
	}

	@Override
	public void finishUpdate(View container) {
	}

	@Override
	public int getCount() {
		return images.length;
	}

	@Override
	public Object instantiateItem(ViewGroup view, int position) {
		View imageLayout = inflater.inflate(R.layout.item_pager_image, view, false);
		ImageView imageView = (ImageView) imageLayout.findViewById(R.id.image);
		final ProgressBar progressBar = (ProgressBar) imageLayout.findViewById(R.id.loading);
		imageView.setOnClickListener(this);
		this.posittion=position;
		imageLoader.displayImage(images[position], imageView, options, new SimpleImageLoadingListener() {
			@Override
			public void onLoadingStarted(String imageUri, View view) {
				progressBar.setVisibility(View.VISIBLE);
			}

			@Override
			public void onLoadingFailed(String imageUri, View view, FailReason failReason) {
				String message = null;
				switch (failReason.getType()) {
					case IO_ERROR:
						message = "Input/Output error";
						break;
					case DECODING_ERROR:
						message = "Image can't be decoded";
						break;
					case NETWORK_DENIED:
						message = "Downloads are denied";
						break;
					case OUT_OF_MEMORY:
						message = "Out Of Memory error";
						break;
					case UNKNOWN:
						message = "Unknown error";
						break;
				}
				Toast.makeText(context, message, Toast.LENGTH_SHORT).show();
				imageShow=false;
				progressBar.setVisibility(View.GONE);
			}

			@Override
			public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {
				imageShow=true;
				progressBar.setVisibility(View.GONE);
			}
		});

		((ViewPager) view).addView(imageLayout, 0);
		return imageLayout;
	}

	@Override
	public boolean isViewFromObject(View view, Object object) {
		return view.equals(object);
	}

	@Override
	public void restoreState(Parcelable state, ClassLoader loader) {
	}

	@Override
	public Parcelable saveState() {
		return null;
	}

	@Override
	public void startUpdate(View container) {
	}
	@Override
	public void onClick(View v) {
		switch(posittion){
		case 1:
			startLoadActivity("2013放心用卡安全支付有奖调查","http://anquan.unionpay.com/");
			break;
		case 2:
			startLoadActivity("最红星期五","http://www.bankcomm.com/BankCommSite/uploadFiles/res/1369876445100/tpyk/index.html");
			break;
		case 3:
			startLoadActivity("最红星期五第二季","http://www.bankcomm.com/BankCommSite/uploadFiles/red.1364896318457/index.html?infoId=1364896443100");
			break;
		}
	}
	
	private void startLoadActivity(String title,String url){
		if(imageShow){
			context.startActivity(new Intent(context,LoadURLWebView.class)
				.putExtra(Extra.WEB_VIEW_TITLE, title)
				.putExtra(Extra.WEB_VIEW_URL, url));
		}
	}
}
